package main

import (
	"fmt"
	"log"
	"math/rand"
	"os/exec"
	"runtime/debug"
	"strconv"
	"time"
)

func init() {
	rand.Seed(time.Now().Unix())
}

func randomStr(min, max int) string {
	return strconv.Itoa(rand.Intn(max-min) + min)
}

func randomDur(min, max int) time.Duration {
	return (time.Duration)(rand.Intn(max-min) + min)
}

func main() {
	defer func() {
		if e := recover(); e != nil {
			log.Printf("%s:%s", e, debug.Stack())
			fmt.Println("Press any key to exit...")
			var c string
			fmt.Scanln(&c)
		}
	}()

	var touchX, touchY string
	var err error

	for {
		// 1.
		touchX, touchY = randomStr(1837, 2082), randomStr(962, 1033)
		_, err = exec.Command("/system/bin/sh", "/system/bin/input", "tap", touchX, touchY).Output()
		if err != nil {
			log.Print(err);
			log.Fatalf("Error tap: %s, %s", touchX, touchY)
		} else {
			log.Printf("再次挑战: (%s, %s)\n", touchX, touchY)
		}

		time.Sleep(time.Millisecond * randomDur(3999, 5099))

		// 2.
		touchX, touchY = randomStr(1523, 1766), randomStr(844, 906)
		_, err = exec.Command("/system/bin/sh", "/system/bin/input", "tap", touchX, touchY).Output()
		if err != nil {
			log.Fatalf("Error tap: %s, %s", touchX, touchY)
		} else {
			log.Printf("闯关: (%s, %s)\n", touchX, touchY)
		}

		time.Sleep(time.Millisecond * randomDur(145666, 156870))

		// 4.done
		touchX, touchY = randomStr(200, 1600), randomStr(250, 1000)
		_, err = exec.Command("/system/bin/sh", "/system/bin/input", "tap", touchX, touchY).Output()
		if err != nil {
			log.Fatalf("Error tap: %s, %s", touchX, touchY)
		} else {
			log.Printf("胜利: (%s, %s)\n", touchX, touchY)
		}

		time.Sleep(time.Millisecond * randomDur(3999, 5099))
	}
}
